from time import sleep
import yaml
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait


def test_login():
    opt = webdriver.ChromeOptions()
    opt.debugger_address = '127.0.0.1:9000'
    driver = webdriver.Chrome(options= opt)
    driver.get('https://work.weixin.qq.com/wework_admin/frame#contacts')
    # datas = driver.get_cookies()
    # with open('datas.yaml', 'w+', encoding='utf-8') as f:
    #     yaml.safe_dump(datas, f)
    #调试代码
    sleep(3)
    driver.find_element_by_xpath('//*[@id="member_list"]/tr[1]/td[1]/input').click()
    sleep(1)
    driver.find_element_by_css_selector('.js_has_member>div:nth-child(1) .js_delete').click()
    sleep(1)
    driver.find_element_by_xpath('//*[@id="__dialog__MNDialog__"]/div/div[3]/a[1]').click()
    sleep(2)


#先获取上面的cookie，再执行以下代码
def test_homework():
    dri = webdriver.Chrome()
    dri.get('https://work.weixin.qq.com')
    dri.maximize_window()
    with open('datas.yaml', encoding='utf-8') as f:
        datas = yaml.safe_load(f)
    for cookie in datas:
        dri.add_cookie(cookie)
    dri.get('https://work.weixin.qq.com/wework_admin/frame#contacts')
    sleep(3)
    dri.find_element_by_css_selector('.js_has_member>div:nth-child(1) .js_add_member').click()
    dri.implicitly_wait(3)
    dri.find_element_by_id('username').send_keys('测试姓名')
    dri.find_element_by_id('memberAdd_english_name').send_keys('测试别名')
    dri.find_element_by_id('memberAdd_acctid').send_keys('ceshizhanghao')
    dri.find_element_by_id('memberAdd_phone').send_keys('11111111111')
    dri.implicitly_wait(3)
    dri.find_element_by_css_selector('.js_member_editor_form>div:nth-child(1) .js_btn_continue').click()
    sleep(2)
    dri.find_element_by_css_selector('.js_member_editor_form>div:nth-child(1) .js_btn_cancel').click()
    dri.implicitly_wait(3)
    element = dri.find_element_by_xpath('//*[@id="member_list"]/tr[1]/td[2]').text
    # print(element) 断言
    assert element == '测试姓名'
    dri.find_element_by_xpath('//*[@id="member_list"]/tr[1]/td[1]/input').click()
    dri.find_element_by_css_selector('.js_has_member>div:nth-child(1) .js_delete').click()
    sleep(1)
    # ddd = dri.switch_to.alert
    # print(ddd)
    # ddd.accept()
    dri.find_element_by_xpath('//*[@id="__dialog__MNDialog__"]/div/div[3]/a[1]').click()
    dri.quit()

